/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package it.coraci.anag.gui.components.specific.gui.docs;

import it.coraci.anag.db.mongo.entities.MongoDocument;
import it.coraci.anag.db.sql.entities.IDDocType;
import it.coraci.anag.db.sql.entities.IDDocument;
import it.coraci.anag.db.sql.entities.Person;
import it.coraci.anag.gui.components.basic.gui.logic.CRUDCentral;
import it.coraci.anag.gui.components.basic.gui.logic.CRUDUpdateEntityListener;
import java.beans.Beans;
import java.util.List;

/**
 *
 * @author Luca
 */
public class NewDocumentsPanel extends javax.swing.JPanel implements CRUDUpdateEntityListener {

    /**
     * Creates new form NewDocumentsPanel
     */
    public NewDocumentsPanel() {
        initComponents();
        if (!Beans.isDesignTime()) {
            System.out.println("---------------------------------------");
            this.cRUDPanel1.setToolbarVisible(false);
            this.cRUDPanel1.prepareForClass(IDDocument.class);
            this.cRUDPanel_docType.setToolbarVisible(false);
            this.cRUDPanel_docType.prepareForClass(MongoDocument.class);
            this.cRUDPanel_docType.setNoSQL(true);
            CRUDCentral.getInstance().addCRUDUpdateEntityListener(this);
        }
    }

    public void clear() {
        this.cRUDPanel1.clear();
        this.cRUDPanel_docType.clear();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        cRUDPanel1 = new it.coraci.anag.gui.components.basic.gui.CRUDPanel();
        cRUDPanel_docType = new it.coraci.anag.gui.components.basic.gui.CRUDPanel();

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(0, 0, 0)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(cRUDPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(cRUDPanel_docType, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(cRUDPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 81, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(cRUDPanel_docType, javax.swing.GroupLayout.DEFAULT_SIZE, 317, Short.MAX_VALUE)
                .addGap(0, 0, 0))
        );
    }// </editor-fold>//GEN-END:initComponents


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private it.coraci.anag.gui.components.basic.gui.CRUDPanel cRUDPanel1;
    private it.coraci.anag.gui.components.basic.gui.CRUDPanel cRUDPanel_docType;
    // End of variables declaration//GEN-END:variables

    @Override
    public void updateThisEntity(Class<?> entityClazz, Object id) {

    }

    @Override
    public void entitySelected(Object entity) {
        if (entity instanceof Person) {
            if (((Person) entity).getIddocuments().isEmpty()) {
                this.clear();
            }
        }
        if (entity instanceof IDDocType) {
            System.out.println("menchiaaaaa");
            List<String> params = ((IDDocType)entity).getParams();
            for (String string : params) {
               this.cRUDPanel_docType.addTextParam(string);
            }
//            String fileID = ((IDDocument) entity).getFileID();
//            if (fileID == null || fileID.isEmpty()) {
//                System.out.println("no file id has been found");
//                return;
//            }
//            MongoDocument findMongoDocument = (MongoDocument) CRUDCentral.getInstance().findEntity(MongoDocument.class, fileID);
//            if (findMongoDocument != null) {
//                this.cRUDPanel_Documents.loadEntity(findMongoDocument);
//            } else {
//                System.out.println("no documents has been found !");
//            }
        }
    }

    @Override
    public void newEntity(Object entityClazz) {
        System.out.println(":D");
    }

}
